package com.adkfp.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.sql.*;

public class DeptNodes {
 
    private String id;
    private String name;
    private Connection conn ;
    
    private String dbdriver ;
    private String dbhost;
    private String dbuser;
    private String dbpassword;

    public DeptNodes() throws IOException, ClassNotFoundException, SQLException  {
    	Properties properties = new Properties();
    	// 使用ClassLoader加载properties配置文件生成对应的输入流
    	//InputStream in = Reader.class.getResourceAsStream("/database.properties"); 
    	InputStream in = Reader.class.getResourceAsStream("/database.properties");
    	// 使用properties对象加载输入流
        properties.load(in);
    	//获取key对应的value值
		
    	Class.forName(properties.getProperty("jdbc.driverClass"));//"com.mysql.jdbc.Driver");
		dbhost =properties.getProperty("jdbc.jdbcUrl") ;//"jdbc:mysql://192.168.0.198:3306/qudao";
		dbuser = properties.getProperty("jdbc.user","root");
		dbpassword = properties.getProperty("jdbc.password", "123456");
		conn=DriverManager.getConnection(dbhost, dbuser, dbpassword);
    }  

	public DeptNodes(String dbdriver, String dbhost, String dbuser, String dbpassword) throws ClassNotFoundException, SQLException {
		super();
		this.dbdriver = dbdriver;
		this.dbhost = dbhost;
		this.dbuser = dbuser;
		this.dbpassword = dbpassword;
    	Class.forName(this.dbdriver);//"com.mysql.jdbc.Driver");
    	conn=DriverManager.getConnection(dbhost, dbuser, dbpassword);
	}

	public void ConnectDB() throws SQLException{
    	conn=DriverManager.getConnection(dbhost, dbuser, dbpassword);
    }
 
    public DeptNodes(String id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    
    protected void finalize() throws SQLException{
    	conn.close();
    }
 
    public String getId() {
        return id;
    }
 
    public void setId(String id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
 
    public List<DeptNodes> getChild(Connection aConn) throws ClassNotFoundException, SQLException, IOException{

        Statement state = aConn.createStatement();
        String SQLStr = " select * from ADK_Dept where PDepID ='"+id+"'";
        //String SQLStr = " select * from ADK_Dept where PDepID ="+id;

        ResultSet rs = state.executeQuery(SQLStr);
        
        List<DeptNodes> progNodes= new ArrayList<DeptNodes>();
        while(rs.next()){
                String progId = rs.getString("DepID");
                String progName = rs.getString("DepName");
                
                //System.out.println(progName);
                if(progId != null && progId != ""){
                	DeptNodes node = new DeptNodes(progId,progName);
                    node.setId(progId);
                    node.setName(progName);
                    progNodes.add(node);
                
            }
        }
        state.close();
        return progNodes;   
    }


    public JSONArray getResult() throws ClassNotFoundException, SQLException, IOException {
 		Statement state = conn.createStatement();
		String SQLStr = " select distinct * from ADK_Dept where PDepID='@'" ;
		ResultSet rs = state.executeQuery(SQLStr);
 
        List<DeptNodes> progNodes= new ArrayList<DeptNodes>();
        while(rs.next()){
            DeptNodes node = new DeptNodes(rs.getString("DepID"),rs.getString("DepName"));//rs.getString("DepID"),rs.getString("DepName"));
            progNodes.add(node);
        }
        state.close();
        
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id","@");
        jsonObject.put("title","全部");
        jsonObject.put("children", getChildInfo(progNodes));
        JSONArray rtJA = new JSONArray();
        rtJA.add(jsonObject); 
        System.out.print(rtJA.toString());
        return rtJA;
    }

    public JSONArray getChildInfo(List<DeptNodes> progNodes) throws ClassNotFoundException, SQLException, IOException{
        JSONArray rtJA = new JSONArray();
        for(DeptNodes prog :progNodes){
        	//System.out.println(prog.name);
            List<DeptNodes> progList = new ArrayList<DeptNodes>();
            progList =prog.getChild(conn);

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id",prog.getId());
            jsonObject.put("title",prog.getName());
            
            if(progList.size()>0){            
                jsonObject.put("children", getChildInfo(progList));
                jsonObject.put("isLast", "false");
            }else
            {
            	jsonObject.put("isLast", "true");
            }
            rtJA.add(jsonObject);
        }
        
        return rtJA;
    }
}
